package com.yuba.front.controller.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.validate.AddGroup;
import com.yuba.common.core.validate.EditGroup;
import com.yuba.common.excel.utils.ExcelUtil;
import com.yuba.common.idempotent.annotation.RepeatSubmit;
import com.yuba.common.log.annotation.Log;
import com.yuba.common.log.enums.BusinessType;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.bo.FrontUserBo;
import com.yuba.front.domain.vo.FrontUserVo;
import com.yuba.front.service.IFrontUserService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理系统的前台用户接口
 *
 * @author modakai
 * @date 2024-11-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/front/frontUser")
public class FrontUserController extends BaseController {

    private final IFrontUserService frontUserService;

    /**
     * 查询前台用户列表
     */
    @SaCheckPermission("front:frontUser:list")
    @GetMapping("/list")
    public TableDataInfo<FrontUserVo> list(FrontUserBo bo, PageQuery pageQuery) {
        return frontUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出前台用户列表
     */
    @SaCheckPermission("front:frontUser:export")
    @Log(title = "前台用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FrontUserBo bo, HttpServletResponse response) {
        List<FrontUserVo> list = frontUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "前台用户", FrontUserVo.class, response);
    }

    /**
     * 获取前台用户详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("front:frontUser:query")
    @GetMapping("/{userId}")
    public R<FrontUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(frontUserService.queryById(userId));
    }

    /**
     * 新增前台用户
     */
    @SaCheckPermission("front:frontUser:add")
    @Log(title = "前台用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FrontUserBo bo) {
        return toAjax(frontUserService.insertByBo(bo));
    }

    /**
     * 修改前台用户
     */
    @SaCheckPermission("front:frontUser:edit")
    @Log(title = "前台用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FrontUserBo bo) {
        return toAjax(frontUserService.updateByBo(bo));
    }

    /**
     * 删除前台用户
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("front:frontUser:remove")
    @Log(title = "前台用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(frontUserService.deleteWithValidByIds(List.of(userIds), true));
    }
}
